﻿using Engine.Graphics;
using Engine;
using Game;

namespace SAGhoul.ParticleSystem
{
	public class SoulsExpParticleSystem : ParticleSystem<SoulsExpParticleSystem.Particle>
	{
		public class Particle : Game.Particle
		{
			public Vector3 Velocity;
			public float TimeToLive;
		}

		public Game.Random m_random = new();
		public SubsystemTerrain m_subsystemTerrain;
		public float m_radius;

		public SoulsExpParticleSystem(SubsystemTerrain terrain, Vector3 position, float radius)
			: base(15)
		{
			m_subsystemTerrain = terrain;
			Texture = ContentManager.Get<Texture2D>("Textures/FireParticle2");
			TextureSlotsCount = 3;
			m_radius = radius;
			for (int i = 0; i < Particles.Length; i++)
			{
				Particle obj = Particles[i];
				obj.IsActive = true;
				obj.Position = position + 0.5f * new Vector3(m_random.Float(-1f, 1f), m_random.Float(-1f, 1f), m_random.Float(-1f, 1f));
				obj.Color = Color.White;
				obj.Size = new Vector2(0.5f);
				obj.TimeToLive = m_random.Float(0.75f, 2f);
				obj.Velocity = new Vector3(3f * m_radius * m_random.Float(-1f, 1f), 2f * m_random.Float(-1f, 1f), 3f * m_radius * m_random.Float(-1f, 1f));
				obj.TextureSlot = 8;
			}
		}

		public override bool Simulate(float dt)
		{
			dt = MathUtils.Clamp(dt, 0f, 0.1f);
			float num = MathUtils.Pow(0.4f, dt);
			bool flag = false;
			for (int i = 0; i < Particles.Length; i++)
			{
				Particle particle = Particles[i];
				if (!particle.IsActive)
				{
					continue;
				}
				flag = true;
				particle.TimeToLive -= dt;
				if (particle.TimeToLive > 0f)
				{
					Vector3 position = particle.Position;
					Vector3 vector = position + particle.Velocity * dt;
					particle.Position = vector;
					particle.Velocity.Y += -10f * dt;
					particle.Velocity *= num;
					particle.TextureSlot = (int)(8.99f * MathUtils.Saturate(2f - particle.TimeToLive));
				}
				else
				{
					particle.IsActive = false;
				}
			}
			return !flag;
		}
	}
}
